package com.xiaoyu.util2;

public class ConvertUtil {
	public static byte[] shortToByte(short number) {
		int temp = number;
		byte[] b = new byte[2];
		for (int i = 0; i < b.length; ++i) {
			b[i] = new Integer(temp & 0xFF).byteValue();
			temp >>= 8;
		}
		return b;
	}

	public static short byteToShort(byte[] b) {
		short s = 0;
		short s0 = (short) (b[0] & 0xFF);
		short s1 = (short) (b[1] & 0xFF);
		s1 = (short) (s1 << 8);
		s = (short) (s0 | s1);
		return s;
	}

	public static byte[] intToByte(int i) {
		byte[] bt = new byte[4];
		bt[0] = (byte) (0xFF & i);
		bt[1] = (byte) ((0xFF00 & i) >> 8);
		bt[2] = (byte) ((0xFF0000 & i) >> 16);
		bt[3] = (byte) ((0xFF000000 & i) >> 24);
		return bt;
	}

	public static int bytesToInt(byte[] bytes) {
		int num = bytes[0] & 0xFF;
		num |= bytes[1] << 8 & 0xFF00;
		num |= bytes[2] << 16 & 0xFF0000;
		num |= bytes[3] << 24 & 0xFF000000;
		return num;
	}

	public static byte[] longToByte(long number) {
		long temp = number;
		byte[] b = new byte[8];
		for (int i = 0; i < b.length; ++i) {
			b[i] = new Long(temp & 0xFF).byteValue();
			temp >>= 8;
		}
		return b;
	}

	public static long byteToLong(byte[] b) {
		long s = 0L;
		long s0 = b[0] & 0xFF;
		long s1 = b[1] & 0xFF;
		long s2 = b[2] & 0xFF;
		long s3 = b[3] & 0xFF;
		long s4 = b[4] & 0xFF;
		long s5 = b[5] & 0xFF;
		long s6 = b[6] & 0xFF;
		long s7 = b[7] & 0xFF;
		s1 <<= 8;
		s2 <<= 16;
		s3 <<= 24;
		s4 <<= 32;
		s5 <<= 40;
		s6 <<= 48;
		s7 <<= 56;
		s = s0 | s1 | s2 | s3 | s4 | s5 | s6 | s7;
		return s;
	}
}